﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abigail.Ui.Article
{
    public partial class UiViewArticleCategories : Form
    {
        Controller c;
        List<Model.ArticleCategory> items;

        public UiViewArticleCategories(Controller c, List<Model.ArticleCategory> items)
        {
            InitializeComponent();
            this.c = c;
            this.items = items;
            buildtree();
            this.Show();
        }

        private void SetSelected(int id)
        {
            int i = 0;
            while (i < tree_categories.Nodes.Count)
                if (((Model.ArticleCategory)tree_categories.Nodes[i].Tag).Id == id)
                    tree_categories.SelectedNode = tree_categories.Nodes[i];
        }

        private void buildtree()
        {
            tree_categories.Nodes.Clear();    // Clear any existing categories
            tree_categories.BeginUpdate();    // prevent overhead and flicker
            LoadBaseNodes();                  // load all the lowest tree nodes
            tree_categories.EndUpdate();      // re-enable the tree
            tree_categories.Refresh();        // refresh the treeview display
        }

        private void LoadBaseNodes()
        {
            int baseParent = 0;                 // Find the lowest root category parent value
            TreeNode node;
            foreach (Model.ArticleCategory cat in items)
            {
                if (cat.MainCategory < baseParent)
                    baseParent = cat.MainCategory;
            }
            foreach (Model.ArticleCategory cat in items)         // iterate through the categories
            {
                if (cat.MainCategory == baseParent)     // found a matching root item
                {
                    node = tree_categories.Nodes.Add(cat.Name); // add it to the tree
                    node.Tag = cat;                 // send the category into the tag for future processing
                    getChildren(node);              // load all the children of this node
                }
            }
        }
        // recursive tree loader. Passes back in a node to retireve its childre
        // until there are no more children for this node.
        private void getChildren(TreeNode node)
        {
            TreeNode Node = null;
            Model.ArticleCategory nodeCat = (Model.ArticleCategory)node.Tag;  // get the category for this node
            foreach (Model.ArticleCategory cat in items)         // locate all children of this category
            {
                if (cat.MainCategory == nodeCat.Id)     // found a child
                {
                    Node = node.Nodes.Add(cat.Name);    // add the child
                    Node.Tag = cat;                         // set its tag to its category
                    getChildren(Node);                      // find this child's children
                }
            }
        }
        private void btn_update_Click(object sender, EventArgs e)
        {
            items = c.GetCategoryList();
            buildtree();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            c.OpenUiNewArticleCategory();
        }

        private void tree_categories_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Model.ArticleCategory cat = (Model.ArticleCategory)tree_categories.SelectedNode.Tag;
            c.OpenUiEditArticleCategory(cat.Id, cat.Name, cat.MainCategory); 
        }
    }
}
